import { 
    createRouter,
    createWebHashHistory
} from 'vue-router'

import Admin from "@/layout/index.vue";
import Index from '@/pages/index.vue'
import Login from '@/pages/login.vue'
import NotFound from '@/pages/404.vue'


//默认路由
const routes = [
    {
        path: "/",
        name:'admin',
        component: Admin,
    },
    {
        path: "/login",
        component: Login,
        meta: {
            title: "登錄頁"
        }
    }, {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }
]

//动态路由
const asyncRoutes = [
    {
        path: "/",
        name:'/',
        component: Index,
        meta: {
            title: "後台首頁"
        }
    }, {
        path: "/product",
        name:'/product',
        component: () => import('@/pages/product/index.vue'),
        meta: {
            title: "產品管理"
        }
    }, {
        path: "/category",
        name:"/category",
        component: () => import('@/pages/category/index.vue'),
        meta: {
            title: "分類管理"
        }
    }, {
        path: "/admin",
        name:"/admin",
        component: () => import('@/pages/admin/index.vue'),
        meta: {
            title: "管理員管理"
        }
    },
    {
        path: "/role",
        name:"/role",
        component: () => import('@/pages/role/index.vue'),
        meta: {
            title: "角色管理"
        }
    },
    {
        path: "/member",
        name:"/member",
        component: () => import('@/pages/member/index.vue'),
        meta: {
            title: "用戶管理"
        }
    },
    {
        path: "/integral",
        name:"/integral",
        component: () => import('@/pages/integral/index.vue'),
        meta: {
            title: "會員等級"
        }
    },
    {
        path: "/notice",
        name:"/notice",
        component: () => import('@/pages/notice/index.vue'),
        meta: {
            title: "推送管理"
        }
    },
    {
        path: "/analyse",
        name:"/analyse",
        component: () => import('@/pages/analyse/index.vue'),
        meta: {
            title: "數據分析"
        }
    },
    {
        path: "/access",
        name:"/access",
        component: () => import('@/pages/access/index.vue'),
        meta: {
            title: "菜單管理"
        }
    },
    {
        path: "/purchase",
        name:"/purchase",
        component: () => import('@/pages/stockpile/purchase.vue'),
        meta: {
            title: "採購記錄"
        }
    },
    {
        path: "/spoilage",
        name:"/spoilage",
        component: () => import('@/pages/stockpile/spoilage.vue'),
        meta: {
            title: "損耗記錄"
        }
    },
    {
        path: "/store",
        name:"/store",
        component: () => import('@/pages/store/index.vue'),
        meta: {
            title: "店鋪設置"
        }
    },
    {
        path: "/banner",
        name:"/banner",
        component: () => import('@/pages/store/banner.vue'),
        meta: {
            title: "banner設置"
        }
    },
    {
        path: "/service",
        name:"/service",
        component: () => import('@/pages/store/service.vue'),
        meta: {
            title: "客服設置"
        }
    },
    {
        path: "/about",
        name:"/about",
        component: () => import('@/pages/store/about.vue'),
        meta: {
            title: "關於我們"
        }
    },
    {
        path: "/query",
        name:"/query",
        component: () => import('@/pages/systems/query.vue'),
        meta: {
            title: "疑問解答"
        }
    },
    {
        path: "/coupon",
        name:"/coupon",
        component: () => import('@/pages/coupon/index.vue'),
        meta: {
            title: "優惠券管理"
        }
    },
    {
        path: "/agreement",
        name:"/agreement",
        component: () => import('@/pages/systems/agreement.vue'),
        meta: {
            title: "用戶協議"
        }
    },
    {
        path: "/order",
        name:"/order",
        component: () => import('@/pages/order/index.vue'),
        meta: {
            title: "訂單管理"
        }
    },
    {
        path: "/calendar",
        name: "/calendar",
        component: () => import("@/pages/order/calendar.vue"),
        meta: {
          title: "日曆訂單",
        },
      },
    {
        path: "/comment",
        name:"/comment",
        component: () => import('@/pages/comment/index.vue'),
        meta: {
            title: "評論管理"
        }
    },
    {
        path: "/appointment",
        name:"/appointment",
        component: () => import('@/pages/order/appointment.vue'),
        meta: {
            title: "線下訂單"
        }
    },
    {
        path: "/log",
        name:"/log",
        component: () => import('@/pages/log/index.vue'),
        meta: {
            title: "活動日誌"
        }
    },
    {
        path: "/graph",
        name:"/graph",
        component: () => import('@/pages/analyse/graph.vue'),
        meta: {
            title: "圖形分析"
        }
    },
    {
        path: "/consume",
        name:"/consume",
        component: () => import('@/pages/analyse/consume.vue'),
        meta: {
            title: "消費分析"
        }
    },
    {
        path: "/paypal",
        name:"/paypal",
        component: () => import('@/pages/store/paypal.vue'),
        meta: {
            title: "消費分析"
        }
    }
]    



export const router = createRouter({
    history:createWebHashHistory(),
    routes
})

//递归菜单 动态添加路由
export function addRoutes(menus){
    //是否有新的路由
    let hasNewRoutes=false
    const findMenus=(arr)=>{
        arr.forEach(e=>{
            let item =asyncRoutes.find(o=>o.path==e.path)
            if(item && !router.hasRoute(item.path)){
                router.addRoute('admin',item)
                hasNewRoutes=true
           }
           if(e.child && e.child.length>0){
            findMenus(e.child)
         }
        })
    }
    findMenus(menus)
   
    return hasNewRoutes
}